/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  SignActivity.java   
 * @Package com.jiyi.activity.new_maintenance   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-8-31 下午5:15:26   
 * @version V1.0     
 */
package com.jiyi.activity.new_maintenance;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jiyi.activity.SinglePicActivity;
import com.jiyi.easemob.util.UserHelp;
import com.jiyi.event.MaintenanceTaskListEvent;
import com.jiyi.inteface.WebApi;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;
import com.swift.utils.CameraUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * @ClassName: SignActivity
 * @Description:TODO(任务签到)
 * @author: HYM
 * @date: 2017-8-31 下午5:15:26
 * 
 */
public class TaskSignActivity extends SinglePicActivity {

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;
	@BindView(R.id.ivPic)
	ImageView ivPic;
	@BindView(R.id.tvTip)
	TextView tvTip;

	private String task_user_id, company_id, company_name;
	// 之前选择的项目position,从哪里来的
	int position = -1, type = -1;
	Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_task_sign);
		ButterKnife.bind(this);
		init();
	}

	private void init() {
		setTop();
		getIntentData();
	}

	private void setTop() {
		tvTitle.setText("签到拍照");
		tvSearch.setCompoundDrawables(null, null, null, null);
		tvSearch.setText("上传");
	}

	private void getIntentData() {
		task_user_id = getIntent().getStringExtra("task_user_id");
		company_id = getIntent().getStringExtra("company_id");
		company_name = getIntent().getStringExtra("company_name");
		position = getIntent().getIntExtra("position", -1);
		type = getIntent().getIntExtra("type", -1);
	}

	@OnClick({ R.id.tvBack, R.id.tvSearch, R.id.ivPic })
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tvBack:
			finish();
			break;
		case R.id.tvSearch:
			if (TextUtils.isEmpty(filePath)) {
				ToastUtil.getInstances().showMsg("请先上传图片");
				return;
			}
			
			doUpload();
			break;
		case R.id.ivPic:
			mPickPopView.showPopWindow();
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == CAMARE_CODE && resultCode == RESULT_OK) {
			try {
				Glide.with(this).load(new File(filePath)).into(ivPic);
				tvTip.setText("已完成");
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (requestCode == CHOOSE_PICTURE) {
			if (resultCode == RESULT_OK) {
				if (data != null) {
					Uri temp = data.getData();
					filePath = CameraUtils.getImageAbsolutePath(this, temp);
					Glide.with(this).load(new File(filePath)).into(ivPic);
					tvTip.setText("已完成");
				}

			}
		}
	}
	
	
	void doUpload()
	{
		String webApi = WebApi.MAINTENANCE_TASK_SIGN;
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		params.put("task_user_id", task_user_id);
		params.put("company_id", company_id);
		params.put("company_name", company_name);

		PostFormBuilder postForm = OkHttpUtils.post().url(webApi);
		postForm.params(params);
		if (!TextUtils.isEmpty(filePath)) {
			File file = new File(filePath);
			postForm.addFile("image", "maintenance_sign.jpg", file);
		}

		dialog = showDialog(dialog);
		postForm.build().execute(new StringCallback() {

			@Override
			public void onError(Call arg0, Exception exception, int arg2) {
				hideDialog(dialog);
				ToastUtil.getInstances().showMsg(exception.getMessage());
			}

			@Override
			public void onResponse(String result, int arg1) {
				try {
					JSONObject obj = new JSONObject(result);
					String status = obj.getString("status");
					String info = obj.getString("info");
					if (status.equals("200")) {
						ToastUtil.getInstances().showMsg(info);
						EventBus.getDefault().post(new MaintenanceTaskListEvent(type, position,1));
						finish();
					} 
					else if (status.equals("404") && info.contains("尚未登录")) {
						ToastUtil.getInstances().showMsg("尚未登录或该用户在其他地方登录，请重新登录！");
						//清除环信登录
						UserHelp.getInstance().clearUser(AppBase.Context, true);
						return;
					}
					else {
						ToastUtil.getInstances().showMsg(info);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				} finally {
					hideDialog(dialog);
				}
			}
		});
	}

}
